package cn.iocoder.yudao.module.worker.api.goods;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.worker.api.goods.dto.WorkerGoodsRespDTO;
import cn.iocoder.yudao.module.worker.controller.admin.category.vo.CategoryPageReqVO;
import cn.iocoder.yudao.module.worker.convert.goods.WorkerGoodsConvert;
import cn.iocoder.yudao.module.worker.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.module.worker.dal.dataobject.goods.WorkerGoodsDO;
import cn.iocoder.yudao.module.worker.service.category.CategoryService;
import cn.iocoder.yudao.module.worker.service.clerk.ClerkService;
import cn.iocoder.yudao.module.worker.service.goods.WorkerGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Validated
public class WorkerGoodsApiImpl implements WorkerGoodsApi {

    @Resource
    private WorkerGoodsService workerGoodsService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private ClerkService clerkService;

    @Override
    public WorkerGoodsRespDTO getGoodsById(Long clerkId, Long goodsId) {
        WorkerGoodsDO workerGoodsDO = workerGoodsService.getGoods(goodsId);
        if(clerkId != null){
            workerGoodsDO = clerkService.getGoodsById(clerkId, goodsId);
        }
        CategoryPageReqVO reqVO = new CategoryPageReqVO();
        List<CategoryDO> categoryDOList = categoryService.getCategoryList(reqVO);
        Map<Long, CategoryDO> categoryMap = CollectionUtils.convertMap(categoryDOList, CategoryDO::getId);

        WorkerGoodsRespDTO respDTO = new WorkerGoodsRespDTO();

        String name = "";
        String picUrl = "";
        List<String> allCategoryNameList = new ArrayList<>();
        if(categoryMap.get(workerGoodsDO.getCategoryParentId()) != null){
            name = categoryMap.get(workerGoodsDO.getCategoryParentId()).getName() + " ";
            picUrl = categoryMap.get(workerGoodsDO.getCategoryParentId()).getThumb();
            allCategoryNameList.add(categoryMap.get(workerGoodsDO.getCategoryParentId()).getName());
        }
        if(categoryMap.get(workerGoodsDO.getCategoryId()) != null){
            name += categoryMap.get(workerGoodsDO.getCategoryId()).getName();
            if(StringUtils.isBlank(picUrl)){
                picUrl = categoryMap.get(workerGoodsDO.getCategoryId()).getThumb();
            }
            allCategoryNameList.add(categoryMap.get(workerGoodsDO.getCategoryId()).getName());
        }
        List<String> categoryNameList = new ArrayList<>();
        categoryNameList.add(workerGoodsDO.getName());
        allCategoryNameList.add(workerGoodsDO.getName());
        respDTO.setId(workerGoodsDO.getId());
        respDTO.setSpuId(clerkId);
        respDTO.setName(name);
        respDTO.setCategoryNameList(categoryNameList);
        respDTO.setAllCategoryNameList(allCategoryNameList);
        respDTO.setPrice(workerGoodsDO.getPrice());
        respDTO.setPicUrl(picUrl);
        return respDTO;
    }

    @Override
    public WorkerGoodsRespDTO getGoodsById(Long goodsId) {
        WorkerGoodsDO goodsDO = workerGoodsService.getGoods(goodsId);
        return WorkerGoodsConvert.INSTANCE.convert(goodsDO);
    }

}
